package com.springboot.demo.count.str;

/**
 * 超大数相加
 *
 * @author supu
 * @date 2020-03-20 14:49
 **/
public class BigNumberAdd {
    public static String addNumbers(int a, int b){
        String aStr = String.valueOf(a);
        String bStr = String.valueOf(b);
        char[] aChars = new StringBuffer(aStr).reverse().toString().toCharArray();
        char[] bChars = new StringBuffer(bStr).reverse().toString().toCharArray();
        int maxlength = Math.max(aChars.length, bChars.length);
        int[] result = new int[maxlength+1];
        int temp = 0;
        for (int i = 0; i < maxlength; i++) {
            temp = result[i];
            if(i < aChars.length){
                temp += aChars[i] - '0';
            }
            if(i < bChars.length){
                temp += bChars[i] - '0';
            }
            if(temp >= 10){
                result[i] = temp % 10;
                result[i+1] = temp / 10;
            } else {
                result[i] = temp;
            }
        }

        StringBuffer resultStr = new StringBuffer();
        for (int i = result.length -1; i >= 0 ; i--) {
            if(i == result.length - 1) {
                if(result[i] > 0 ){
                    resultStr.append(result[i]);
                }
            } else {
                resultStr.append(result[i]);
            }
        }
        return resultStr.toString();
    }

    public static void main(String[] args) {
        System.out.println(BigNumberAdd.addNumbers(1234566778,546576586));
    }
}
